<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>FilePremier - Plugin</title>
<link rel="shortcut icon" href="favicon.ico">
<link rel="stylesheet" type="text/css" href="about_filepremier.css"/>

<script type="text/javascript">

var numb;

chrome.extension.onRequest.addListener(
  function(request, sender, callback) {
  	console.log("about_action: " + request.action);
    if (request.action == 'load'){
        var i;
        for(i=0; i <request.code.length; i++){
          var ni = document.getElementById('about-debrid-file-links');
          var newdiv = document.createElement('div');
          if(request.code[i] != request.code[i+1]){
              newdiv.innerHTML = request.code[i];
              ni.appendChild(newdiv);
          }
		  console.log("request.code: " + request.code[i]);
        }
		genLinks(0);
    }
    else
      callback({});
});

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
	console.log(changeInfo.status);
	if(changeInfo.status == "complete") chrome.extension.sendRequest({action:'genLinks'});
});

function folderCheckLink(link) {
	var folder = new Array(
		/http:\/\/(www\.)?megaupload\.com(\/[a-z]{2})?\/\?f=[0-9a-zA-Z]+/g,
		/http:\/\/(\w+\.)?netfolder\.in\/(folder\.php\?folder_id\=(\w+)|([0-9a-zA-Z]+)\/[0-9]+)/g,
		/http:\/\/(www\.)?hotfile\.com\/list\/[^( |"|>)]+/g,
		 /http:\/\/(www\.)?uploadstation\.com\/list\/[^( |"|>)]+/g,
		 /http:\/\/(www\.)?filesonic\.(com|it|de|es|fr|hk|jp)\/folder\/[^( |"|>)]+/g,
		/http:\/\/(\w+\.)?(wupload)\.(com|fr|de|it|net|org|jp)\/folder\/[^\"\r\n< ]+/g,
		/http:\/\/(www\.)?fileserve\.com\/list\/[^( |"|>)]+/g,
		/http:\/\/(\w+\.)?linksave\.in\/([0-9a-zA-Z]+)/g
		);
	
    if(!link) return 0;
    var i;    
    for(i=0;i<folder.length;i++) {
		var res = link.match(folder[i]);
		if(res) return res;
		}    return 0;
}

function genMultiLinks(links,index)
{
	console.log("genMultiLinks links[0]: " + links[0]);
	var xhttpreqs = new XMLHttpRequest();
	xhttpreqs.onreadystatechange = function(evt){
		if (xhttpreqs.readyState == 4) {
			if (xhttpreqs.status == 200) {
				var lin=xhttpreqs.responseText;
				console.log("genMultiLinks response: " + lin);
				if(lin == "ERROR: account_expired"){
					document.getElementById('nopremium').style.display = 'block';
				}
				else{
					if(lin == "ERROR: not_available")
						numb[index].innerHTML = "<font color='red'>File not available</font>";
					else if(lin=="ERROR: not_supported")
						numb[index].innerHTML = "<font color='red'>Unsupported host or bad typed link</font>";
					else if(lin=="ERROR: missing_parameters")
					{
						numb[index].innerHTML = "<font color='red'>You're not logged in. Click <a href='http://www.filepremier.com'>here</a> to login</a>";
						return;
					}
					else
						numb[index].innerHTML = '<a href="' + lin + '" target="blank">' + lin + '</a><br />';
					
					if((links.length-1)>0){
						links.shift();
						index++;
						genMultiLinks(links,index);
					}
				}
			}
		}
	}
	xhttpreqs.open("GET","http://www.filepremier.com/api.php?generate=&link="+escape(links[0]),true);
	xhttpreqs.send("");
}


function genFolderLinks(links,index,iteration)
{
	console.log("genFolderLinks links[0]: " + links[0]);
	if(iteration==0)
		numb[index].innerHTML='';
		
	var xhttpreqs = new XMLHttpRequest();
	xhttpreqs.onreadystatechange = function(evt){
		if (xhttpreqs.readyState == 4) {
			if (xhttpreqs.status == 200) {
				var lin=xhttpreqs.responseText;
				console.log("genFolderLinks response: " + lin);
				if(lin == "ERROR: account_expired"){
					document.getElementById('nopremium').style.display = 'block';
				}
				else
				{
					if(lin == "ERROR: not_available" || lin=="ERROR: not_supported")
						numb[index].innerHTML = lin;
					else
						numb[index].innerHTML += '<a href="' + lin + '" target="blank">' + lin + '</a><br />' ;
									
					if((links.length-2)>0){ //links.length-2 because the folder_links has one element that is just a blank space
						links.shift();
						genFolderLinks(links,index,1);
					}
				}
			}
		}
	}
	xhttpreqs.open("GET","http://www.filepremier.com/api.php?generate=&link="+escape(links[0]),true);
	xhttpreqs.send("");
}

function genLinks(x) {
    var plgv = chrome.i18n.getMessage("pref_version_num_text");
    document.getElementById('about-debrid-version').innerHTML = plgv;
    numb = document.getElementById('about-debrid-file-links').getElementsByTagName("div");
    var i;
	var xhttp = new XMLHttpRequest();
	
    for(i=0; i<numb.length; ++i){
		if(folderCheckLink(numb[i].innerHTML)) {
			var index = i;
			console.log("folder: " +i + " " + numb[i].innerHTML);
			
			xhttp.open("GET","http://www.filepremier.com/api.php?folder&l="+escape(numb[i].innerHTML),true);
			xhttp.send("");
			xhttp.onreadystatechange = function(aEvt) {
				if (xhttp.readyState == 4) {
					if (xhttp.status == 200) {
						var res = xhttp.responseText;
						if(!res) numb[index].innerHTML = '';
						var folder_links = res.split('\n');
						console.log(numb[index].innerHTML);
						genFolderLinks(folder_links,index,0);
					}
				} 
			}
		}
		else{ 
			var links = [];
			for(var j=0; j<numb.length; j++)
			{
				if(!folderCheckLink(numb[j].innerHTML))
				{
					links.push(numb[j].innerHTML);
				}
			}
			genMultiLinks(links,i);
			return;
		}
     }
}

</script>
</head>
<body>
    <div id="wrapper">
       <div id="preamble">
        <h2>&nbsp;&nbsp;&nbsp;FilePremier - Plugin <span id="about-debrid-version"></span></h2>
         <div id="preamblemain" class="blockmain">
          <div id="preamblentry" class="blockentry">
           <p class="pre1">
	       <table width="860" border="0">
            <tr>
                <td width="25%" valign="top">
                    <center>
					<img src="../images/icon100.png" alt="Download" />
					</center>
                </td>
                <td width="75%">
                    <div id="about-debrid-file-links">
					<span id="nopremium" style="display: none"><font color="red"><b><span id="msg7"><script>document.write(chrome.i18n.getMessage("about_debrid_premium_text"))</script></span></b></font><br /><img src="../images/coins_add.png">&nbsp;&nbsp;<a href="http://www.filepremier.com/premium.php" target="_blank"><span id="msg8"><script>document.write(chrome.i18n.getMessage("about_debrid_offers_text"))</script></span></a><br /><br />
                    </div>
                </td>
            </tr>
	       </table>
           </p>
           <br />
           <br />
          </div>
         </div>
       </div>
    </div>
</body>
</html>